package com.beiing.tianshuai.tianshuai.mine.model;

import com.beiing.tianshuai.tianshuai.entity.CodeBean;
import com.beiing.tianshuai.tianshuai.entity.MyInterestVideoBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by Jinni on 2017/12/8 0008
 */

public class MyInterestContentModel implements MyInterestContentModelImpl{

    private static final String TAG = "MyInterestContentModel";
    private OnMyInterestContentListener mListener;

    public MyInterestContentModel(OnMyInterestContentListener listener) {
        mListener = listener;
    }

    @Override
    public void getMyVideoContent(String uid, String pageSize, int page, int status) {
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("uid", uid)
                .addFormDataPart("pageSize", pageSize)
                .addFormDataPart("page", String.valueOf(page))
                .addFormDataPart("status", String.valueOf(status))
                .build();
        Observable<MyInterestVideoBean> observable = DataManager.getMyVideoContent(body);
        Observable<MyInterestVideoBean> observeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observeOn.subscribe(new DisposableObserver<MyInterestVideoBean>() {
            @Override
            public void onNext(MyInterestVideoBean bean) {
                if (mListener != null) {
                    mListener.getMyVideoContentSuccess(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                if (mListener != null) {
                    LogUtils.e(e.getMessage(), TAG);
                    mListener.getMyVideoContentError(e);
                }
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void getDeleteResult(String uid, String vid) {
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("uid", uid)
                .addFormDataPart("id", vid)
                .build();
        Observable<CodeBean> observable = DataManager.getMyVideoDeleteResult(body);
        Observable<CodeBean> observeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observeOn.subscribe(new DisposableObserver<CodeBean>() {
            @Override
            public void onNext(CodeBean bean) {
                if (mListener != null) {
                    mListener.onDeleteSuccess(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                if (mListener != null) {
                    LogUtils.e(e.getMessage(), TAG);
                    mListener.onDeleteError(e);
                }
            }

            @Override
            public void onComplete() {

            }
        });
    }

    public interface OnMyInterestContentListener{
        void getMyVideoContentSuccess(MyInterestVideoBean bean);
        void getMyVideoContentError(Throwable throwable);
        void onDeleteSuccess(CodeBean bean);
        void onDeleteError(Throwable throwable);
    }
}
